Skip to content

Conversation

@fenilfaldu
Copy link
Contributor

📥 Pull Request

📘 Description
Added async context manager support to handle span lifecycles in async operations, ensures proper cleanup and preventing premature span exports.

🧪 Testing
Verified span lifecycle management in async contexts and confirmed backward compatibility with existing sync initialization.

@fenilfaldu fenilfaldu requested review from dot-agi and tcdent May 13, 2025 23:15
@codecov
Copy link

codecov bot commented May 13, 2025

Codecov Report

Attention: Patch coverage is 63.63636% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
agentops/sdk/decorators/factory.py 63.63% 4 Missing ⚠️

📢 Thoughts on this report? Let us know!

@bboynton97 bboynton97 requested a review from Dwij1704 May 14, 2025 02:49
@areibman areibman linked an issue May 14, 2025 that may be closed by this pull request
3 tasks
@Dwij1704
Copy link
Member

@fenilfaldu I have merged changes related to linting, please refactor to pass unit test :)

@Dwij1704 Dwij1704 requested a review from Copilot May 14, 2025 15:28
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces async context manager support into the agent decorator to ensure proper span lifecycle cleanup during asynchronous operations. The key changes are:

  • Addition of a new test in tests/unit/sdk/test_decorators.py to verify async context manager behavior and object deletion.
  • Updates in agentops/sdk/decorators/factory.py to implement async aenter, async aexit, and enhanced del logic for robust span cleanup.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/unit/sdk/test_decorators.py Added tests for async context manager and del cleanup logic.
agentops/sdk/decorators/factory.py Updated the decorator factory to support async context management.
Comments suppressed due to low confidence (1)

agentops/sdk/decorators/factory.py:77

  • [nitpick] Since aexit is implemented as an async method, verify that calling the synchronous exit method on the span context manager does not block the event loop. If the underlying span context manager supports async cleanup, consider using it instead.
self._agentops_span_context_manager.__exit__(exc_type, exc_val, exc_tb)

Copy link
Member

@Dwij1704 Dwij1704 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can safely ditch del here - the context managers in init and aexit have got our backs for both sync and async cleanup. It's like having two people doing the same job - might as well let the more reliable one handle it!

@fenilfaldu fenilfaldu requested a review from Dwij1704 May 14, 2025 18:06
@dot-agi dot-agi requested a review from Copilot May 14, 2025 22:27
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds async context manager support to class decorators for proper span lifecycle handling in asynchronous workflows and includes a new test to cover async enter/exit and destructor cleanup.

  • Introduce __aenter__ and __aexit__ in WrappedClass for async context manager support.
  • Remove the existing __del__ method (unintentionally dropping sync cleanup).
  • Add a new async test test_async_context_manager_and_del_coverage in tests/unit/sdk/test_decorators.py.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
tests/unit/sdk/test_decorators.py Added gc, pytest imports and test_async_context_manager_and_del_coverage.
agentops/sdk/decorators/factory.py Removed __del__; added __aenter__/__aexit__ methods to support async usage.
Comments suppressed due to low confidence (3)

tests/unit/sdk/test_decorators.py:632

  • [nitpick] After forcing garbage collection, add assertions to verify that the instance's span references (_agentops_span_context_manager, _agentops_active_span) have been cleared to confirm proper cleanup.
gc.collect()  # Force garbage collection to trigger __del__

tests/unit/sdk/test_decorators.py:2

  • [nitpick] Group and sort standard library imports together (e.g., asyncio, gc) and separate them from third-party imports (pytest), following PEP8 import ordering.
import asyncio

agentops/sdk/decorators/factory.py:53

  • The original __del__ destructor was removed, so spans started in __init__ may never be closed for non-async usage. Reintroduce a __del__ method that calls the context manager's __exit__ to ensure proper cleanup in synchronous scenarios.
async def __aenter__(self):

@dot-agi dot-agi requested a review from Copilot May 14, 2025 22:29
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This pull request adds async context manager support for span lifecycles in async operations to better ensure proper cleanup and prevent premature span exports. Key changes include:

  • Adding async aenter/aexit methods to the decorated classes.
  • Updating tests to cover async context management and cleanup logic.
  • Removing the synchronous del cleanup in favor of async cleanup.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/unit/sdk/test_decorators.py Added tests for the new async context management support.
agentops/sdk/decorators/factory.py Updated the decorator to support async lifecycle management by implementing aenter and aexit.

Copy link
Member

@Dwij1704 Dwij1704 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Dwij1704 Dwij1704 merged commit f77031d into AgentOps-AI:main May 15, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: @agent decorator doesn't work + not helpful

2 participants